package cn.itsource.pethome.basic.controller;


import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FastdfsController {

    @PostMapping("/fastDfs")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取上传的文件名 1.jpg
            String originalFilename = file.getOriginalFilename();
            //获取文件后缀名
            String extension = FilenameUtils.getExtension(originalFilename);
            System.out.println(extension);
            //返回fastdfs对应的图片路径
            String uploadPath = FastDfsApiOpr.upload(file.getBytes(), extension);
            return new AjaxResult().setResult(uploadPath);
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage()) ;
        }
    }

}
